package com.example.androidframework.ui.splash

import android.annotation.SuppressLint
import android.os.Bundle
import android.os.CountDownTimer
import android.os.Handler
import android.os.Looper
import android.os.Message
import androidx.recyclerview.widget.PagerSnapHelper
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.OnScrollListener
import com.bumptech.glide.Glide
import com.example.androidframework.MyApplication
import com.example.androidframework.R
import com.example.androidframework.databinding.ActivitySplashBinding
import com.example.androidframework.ui.main.MainActivity
import com.example.androidframework.ui.privacy.FirstPrivacyPopView
import com.lxj.xpopup.XPopup
import com.martin.lib_base.base.BaseActivity
import com.martin.lib_base.constant.ConstantKey
import com.martin.lib_base.ktx.loge
import com.martin.lib_base.utils.UserUtil
import com.tencent.mmkv.MMKV
import com.xuexiang.xui.utils.StatusBarUtils

@SuppressLint("CustomSplashScreen")
class SplashActivity : BaseActivity<ActivitySplashBinding, SplashViewModel>() {

    private val handler: Handler by lazy {
        @SuppressLint("HandlerLeak")
        object : Handler() {
            override fun handleMessage(msg: Message) {
                super.handleMessage(msg)
                if (msg.what == 1) {
                    // 开启进入首页倒计时
                    timer.start()
                }
            }
        }
    }

    // 定时器
    private val timer: CountDownTimer by lazy {
        object : CountDownTimer(4 * 1000, 1000) {
            // 间隔触发
            @SuppressLint("SetTextI18n")
            override fun onTick(millisUntilFinished: Long) {
                loge("SplashActivity ------ timer onTick: $millisUntilFinished")
                runOnUiThread {
                    binding.numTv.text = "${millisUntilFinished / 1000}s"
                }
            }
            // 结束
            override fun onFinish() {
                loge("SplashActivity ------ timer onFinish")
                finishActivity()
            }

        }
    }

    // 滚动监听
    private val scrollListener: OnScrollListener by lazy {
        object : RecyclerView.OnScrollListener() {
            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)
            }

            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
            }
        }
    }



    override fun getViewModelClass(): Class<SplashViewModel> {
        return SplashViewModel::class.java
    }

    override fun layoutId(): Int {
        return R.layout.activity_splash
    }

    override fun beforeSetContentView() {
        super.beforeSetContentView()
        StatusBarUtils.initStatusBarStyle(this, true)
    }

    override fun initOnCreate(savedInstanceState: Bundle?) {
        loge("SplashActivity ------ initOnCreate")
        // 加载GIF启动图
        Glide.with(this).asGif().load(R.drawable.splash_img_gif).into(binding.img)

        if (!MMKV.defaultMMKV().getBoolean(ConstantKey.FIRST_PRIVACY, false)) {
            // 首次启动用户隐私协议弹窗
            XPopup.Builder(this).dismissOnTouchOutside(false).dismissOnBackPressed(
                false
            ).asCustom(FirstPrivacyPopView(this, onCanel = {
                finish()
            }, onSubmit = {
                MMKV.defaultMMKV().putBoolean(ConstantKey.FIRST_PRIVACY, true).commit()
                // 初始化数据
                initData()
            })).show()
        } else {
            // 初始化数据
            initData()
        }

        binding.viewModel = viewModel
        // 预加载
        PagerSnapHelper().attachToRecyclerView(binding.rv)
        // 添加滚动监听器
        binding.rv.addOnScrollListener(scrollListener)

        // 跳过
        // 李跳跳App会导致此代码直接执行跳过此页面，不是bug
        binding.skipBtn.setOnClickListener {
            loge("点击跳过按钮")
            timer.cancel()
            finishActivity()
        }

        // 开启进入首页倒计时
        timer.start()
    }


    private fun initData() {
        MyApplication.MyApp?.initData()
        UserUtil.initUser()
    }

    private fun finishActivity() {
        loge("SplashActivity ------ finishActivity")
        MainActivity.start(this@SplashActivity)
        finish()
    }

    override fun onDestroy() {
        super.onDestroy()
        // 移除滚动监听器
        binding.rv.removeOnScrollListener(scrollListener)
    }
}